package top.alanshelby.jdbc;

import java.sql.*;

/**
 * JDBC测试类
 */
public class JdbcTest {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jg_test", "root", "123456");
        connection.setAutoCommit(false);
        PreparedStatement statement = connection.prepareStatement("INSERT INTO dept VALUES (?,?,?)");
        statement.setInt(1, 1001);
        statement.setString(2, "test");
        statement.setString(3, "test");
        try {
            statement.execute();
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            e.printStackTrace();
        } finally {
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
    }

}
